package com.zhengtoon.mctoon.conv.controller;

import com.bjtoon.framework.entity.ResponseResult;
import com.zhengtoon.mctoon.conv.dto.response.SlideshowListDto;
import com.zhengtoon.mctoon.conv.dto.response.SubjectConvPhotoDto;
import com.zhengtoon.mctoon.conv.dto.response.SubjectDetailDto;
import com.zhengtoon.mctoon.conv.exception.ConvPhotoExceptionCode;
import com.zhengtoon.mctoon.conv.utils.SessionInfo;
import com.zhengtoon.mctoon.conv.service.ConvPhotoInfoService;
import com.zhengtoon.mctoon.conv.service.ConvSubjectService;
import com.zhengtoon.mctoon.conv.vo.SubjectConvListVo;
import com.zhengtoon.mctoon.conv.vo.SubjectDetailVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 专题管理
 *
 * @author 144637
 */
@RestController
@RequestMapping("/convSubject")
@Api(value = "convSubject", description = "convSubject")
public class ConvSubjectController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(ConvSubjectController.class);

    @Autowired
    private ConvSubjectService convSubjectService;

    @Autowired
    private ConvPhotoInfoService convPhotoInfoService;

    /**
     * 获取专题轮播图列表
     *
     * @return ResponseResult<SubjectManageListDto>
     * @author 144637
     */
    @ApiOperation(value = "获取专题轮播图列表", httpMethod = "POST", notes = "获取专题管理列表")
    @RequestMapping(value = "/getSlideshowList", method = RequestMethod.POST)
    public ResponseResult<List<SlideshowListDto>> getSlideshowList() {
        try {

            List<SlideshowListDto> slideshowListDtoList = convSubjectService.getSlideshowList();

            return ResponseResult.success(slideshowListDtoList);

        } catch (Exception e) {
            log.error("getSlideshowList()方法error",e);
            return ResponseResult.fail("", "获取专题轮播图列表失败");
        }
    }

    /**
     * 获取专题详情
     *
     * @return ResponseResult<SubjectManageListDto>
     * @author 144637
     */
    @ApiOperation(value = "获取专题详情", httpMethod = "POST", notes = "获取专题详情")
    @RequestMapping(value = "/querySubjectDetail", method = RequestMethod.POST)
    public ResponseResult<SubjectDetailDto> querySubjectDetail(
            @ApiParam(required = true, name = "pageVo", value = "分页参数")
            @RequestBody @Valid SubjectDetailVo subjectDetailVo) {
        try {

            Long subjectId = subjectDetailVo.getSubjectId();

            if (subjectId == null) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_FAIL);
            }

            SubjectDetailDto subjectDetailDto = convSubjectService.querySubjectDetail(subjectId);

            return ResponseResult.success(subjectDetailDto);

        } catch (Exception e) {
            log.error("querySubjectDetail()方法error", e);
            return ResponseResult.fail("", "获取专题轮播图列表失败");
        }
    }

    /**
     * 获取专题下的随手拍列表
     *
     * @return ResponseResult<SubjectManageListDto>
     * @author 144637
     */
    @ApiOperation(value = "获取专题下的随手拍列表", httpMethod = "POST", notes = "获取专题下的随手拍列表")
    @RequestMapping(value = "/getSubjectConvList", method = RequestMethod.POST)
    public ResponseResult<List<SubjectConvPhotoDto>> getSubjectConvList(
            HttpServletRequest request,
            @ApiParam(required = true, name = "pageVo", value = "分页参数")
            @RequestBody @Valid SubjectConvListVo subjectDetailVo) {
        try {

            String subjectId = subjectDetailVo.getSubjectId();

            if (StringUtils.isBlank(subjectId)
                    || subjectDetailVo.getPageNum() == null
                    || subjectDetailVo.getPageSize() == null) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_FAIL);
            }

            SessionInfo sessionInfo = getSessionInfo(request);

            List<SubjectConvPhotoDto> subjectConvDtoList = convPhotoInfoService.getSubjectConvList(
                    subjectDetailVo.getPageNum(), subjectDetailVo.getPageSize(), subjectId, sessionInfo.getUserId());

            return ResponseResult.success(subjectConvDtoList);

        } catch (Exception e) {
            log.error("getSubjectConvList()方法error",e);
            return ResponseResult.fail("", "获取专题下的随手拍列表失败");
        }
    }

}